package com.gla.Controller;

import com.gla.domain.Shop;
import com.gla.domain.ShoppingCartItem;
import com.gla.result.Result;
import com.gla.service.ShoppingCartService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/shoppingcart")
public class ShoppingCartController {
    @Resource
    ShoppingCartService shoppingCartService;

    @GetMapping("/getByUserId")
    public Result getShoopingCartByUserId(@RequestParam Integer user_id){
        List<ShoppingCartItem> list =  shoppingCartService.selectByUserId(user_id);
        return Result.succeed(list);
    }

    @GetMapping("/deleteById")
    public Result delete(@RequestParam Integer id){
        shoppingCartService.delete(id);
        return Result.succeed("删除成功");
    }

    @GetMapping("/insert")
    public Result insert(@RequestParam Integer commodity_id,@RequestParam Integer user_id, Integer number){
        if(number==null){
            number = 1;
        }
        return shoppingCartService.insert(commodity_id, user_id,number);
    }

    @GetMapping("/reduceById")
    public Result reduce(@RequestParam Integer id){
        shoppingCartService.updateNumber(id,-1);
        return Result.succeed("数量减少1");
    }

    @GetMapping("/getShopInfo")
    public Result getShopInfo(@RequestParam Integer shop_id){
        Shop shop = shoppingCartService.selectShop(shop_id);
        return Result.succeed(shop);
    }
}
